/**
 * Created by Adam on 2018/5/20 0:36.
 */
$(function () {

    $(document).off('click', '.refresh-btn').on('click', '.refresh-btn', function () {
        var data = $(this).data();
        $.ajax({
            url: data.url,
            success: function (d) {
                if (d.code == 1) {
                    successMsg(d.msg);
                } else {
                    errorMsg(d.msg);
                }
                //如果有设置跳转则跳转到对应url
                if (d.url) {
                    setTimeout(function () {
                        window.location.href= d.url;
                    },1000);
                }
            }
        });
    });

    //菜单跳转或新增修改按钮跳转，检测是否连接跳转或异步刷新右侧视图
    $(document).off('click', '.menu-url,.add-btn').on('click', '.menu-url,.add-btn', function () {
        var data = $(this).data();
        var url = data.url;
        var isframe = data.isframe;
        if (url) {
            if (isframe) {
                window.location.href = url;
            } else {
                $.ajax({
                    url: url,
                    data: data,
                    type: 'post',
                    dataType: 'json',
                    success: function (d) {
                        if(d.code == 0){
                            errorMsg(d.msg);
                            if(d.url){
                                setTimeout(function () {
                                    window.location.href= d.url;
                                },1000);
                            }
                        }else{
                            $('#layout_content').html(d);
                        }
                    }
                });
                return false;
            }
        } else {

        }
    });

    /**
     * 分页跳转
     * 将内置的bootstrap分页跳转 转化为 异步请求 刷新列表 每次覆盖都清除掉上次的点击事件，重新绑定新的点击事件
     */
    $(document).off('click', '.pagination a').on('click', '.pagination a', function () {
        var url = $(this).attr('href');
        var data = urlToJson($('#layout_content form').serialize().replace(/\+/g, " "));
        delete data.act;
        $.post(url, data, function (d) {
            $('#layout_content').find('.table-responsive').html($(d).find('.table-responsive').html());
        });
        return false;
    });

    /**
     * 列表搜索
     */
    $(document).on('submit', '.search-form', function () {
        var data = urlToJson($(this).serialize().replace(/\+/g, " "));
        var url = data.act;
        delete data.act;
        $.post(url, data, function (d) {
            $('#layout_content').find('.table-responsive').html($(d).find('.table-responsive').html());
        });
        return false;
    });

    /**
     * 表单提交
     */
    $(document).on('submit', '.submit-form', function () {
        var data = urlToJson($(this).serialize().replace(/\+/g, " "));
        var url = data.act;
        delete data.act;
        sendAjaxBtn(url, data);
        return false;
    });

    $(document).off('click', '.del-btn').on('click', '.del-btn', function () {
        var data = $(this).data();
        var url = data.url;
        sendAjaxBtn(url, data)
        return false;
    });

    function sendAjaxBtn(url, data) {
        $.post(url, data, function (d) {
            if (d.code == 1) {
                successMsg(d.msg);
            } else {
                errorMsg(d.msg);
            }
            //如果有设置跳转则跳转到对应url
            if (d.url) {
                $.post(d.url, {}, function (page) {
                    setTimeout(function () {
                        $('#layout_content').html(page);

                    }, 1000);
                });
            }
        });
    }

});
